package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Supplier;
import cn.wolfcode.wms.mapper.SupplierMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    public List<Supplier> listAll() {
        return supplierMapper.selectAll();
    }

    public Supplier getById(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(Supplier supplier) {
        if (supplier.getId() != null) {
            supplierMapper.updateByPrimaryKey(supplier);
        } else {
            supplierMapper.insert(supplier);
        }
    }

    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        int rows = supplierMapper.queryForCount(qo);
        if(rows==0){
            return PageResult.empty(qo.getPageSize());
        }
        List<?> result= supplierMapper.queryForList(qo);
        return new PageResult(rows, result,qo.getCurrentPage(),qo.getPageSize());
    }
}
